﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace CSharpUi_ClientShell
{
    public partial class FrmClient : Form
    {

        Process mp = new Process();
        private enum Enum_TunnelState
        {
            Running,
            Stopped
        }
        private Enum_TunnelState me_TunnelState = Enum_TunnelState.Stopped;

        public FrmClient()
        {
            InitializeComponent();
        }

        private void FrmClient_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 启动 tunnel,影响变量 me_TunnelState 
        /// </summary>
        /// <returns>启动成功或者失败</returns>
        private bool  mm_StartTunnel()
        {
            
            //检查操作者是否填写完整

            if (txtServerIp.Text.Length  == 0)
            {
                MessageBox.Show("请填写代理服务器 IP ");
                return false;
            }

            if (txtServerPort.Text.Length  == 0)
            {
                MessageBox.Show("请填写代理服务器 Port");
                return false;
            }

            if (txtServerSecretKey.Text.Length  == 0)
            {
                MessageBox.Show("请填写 key");
                return false;
            }

            #region From document
            //-c              client mode 以客户端方式运行（默认模式）
            //-s              server mode 以服务端方式运行
            //-l              listen port 监听的端口号
            //-i              connect ip address 连接的ip地址
            //-p              connect port 连接的端口号
            //-w              band width limit(Byte/s) 带宽的限制（默认102400）
            //-b              bind hostname 绑定的主机名
            //-k              serect key 密钥
            // in client mode: ./encryptedtunnel -c -k 0514219fbc34 -i 1.1.1.1 -l 12345 -p 1111 -w 102400
            #endregion
            mp.StartInfo.FileName = "encryptedtunnel.exe";//需要启动的程序名       
            mp.StartInfo.Arguments = "-c -k " + txtServerSecretKey.Text
                                    + " -i " + txtServerIp.Text
                                    + " -p " + txtServerPort.Text
                                    + " -w " + 102400
                                    + " -l " + txtListenPort.Text;
            mp.StartInfo.CreateNoWindow = false;
            if (chkOptionShowTunnelWindow.Checked)
            {
                mp.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
            }
            else
            {
                mp.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            }
            txtServerSecretKey.Text = mp.StartInfo.Arguments;

            mp.Start();//启动 tunnel
            if (1 == 1)
            {
                //启动成功
                me_TunnelState = Enum_TunnelState.Running;
                return true;
            }
            return false;
        }
        private void btnLauch_Click(object sender, EventArgs e)
        {
            switch (me_TunnelState)
            {
                case Enum_TunnelState.Running:
                    mp.Kill();
                    me_TunnelState = Enum_TunnelState.Stopped;
                    btnLauch.Text = "Launch";
                    break;
                case Enum_TunnelState.Stopped:
                    if (mm_StartTunnel())
                        btnLauch.Text = "Stop";
                    break;
                default:
                    MessageBox.Show("Update code here 0910-1719");
                    break;
            }
            
        }

        private void barBandWidth_Scroll(object sender, EventArgs e)
        {
            MessageBox.Show("Not implicate");
        }

        private void tmrMain_Tick(object sender, EventArgs e)
        {
            
        }
    }
}
